package cs.study.base.sync007;


/**
 * volatile关键字
 * 子线程子运行时，会将主线程中的变量拷贝一份到自己的独立空间中，这样加快了运行速度。
 */
public class RunThread extends Thread{

	//如果变量被volatile修饰了，那么当变量发生改变时会强制线程执行引擎去主线程的内存中读取
	//volatile值具备可见性，不具备原子性
	private volatile boolean isRunning = true;
	private void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}
	
	public void run(){
		System.out.println("进入run方法..");
		int i = 0;
		while(isRunning == true) {
			//..
		}
		System.out.println("线程停止");
	}
	
	public static void main(String[] args) throws InterruptedException {
		RunThread rt = new RunThread();
		rt.start();
		Thread.sleep(1000);
		rt.setRunning(false);
		System.out.println("isRunning的值已经被设置了false");
		Thread.sleep(1000);
		System.out.println("isRunning值：" + rt.isRunning);
	}
	
}
